package cn.line5.seam.cms.column;

import java.util.List;

import cn.line5.organization.OrganizationConstants;
import cn.line5.organization.OrganizationVo;
import cn.line5.seam.annotations.Name;
import cn.line5.seam.db.PageResult;
import cn.line5.seam.db.Query;
import cn.line5.seam.db.QueryFactory;

@Name("columnDao")
/**
 * <h1>LSEAM框架示例数据库实现类</h1>
 * <br/>
 * 
 * @author Liu,yandong
 */
public class ColumnDaoImpl implements ColumnDao {
	
	public List<ColumnVo> getList() {
		Query query = QueryFactory.createQuery(
						"SELECT "
						+" COLUMN_ID as columnId, "
						+" COLUMN_PARENT as columnParent, "
						+" COLUMN_NAME as columnName, "
						+" COLUMN_INTRODUCE as columnIntroduce, "
						+" COLUMN_ENGLISH as columnEnglish, "
						+" COLUMN_SORT as columnSort, "
						+" COLUMN_URL as columnUrl, "
						+" COLUMN_TYPE as columnType, "
						+" COLUMN_STATE as columnState "
						+" FROM CMS_COLUMN "
						+" ORDER BY COLUMN_SORT ASC ");
		List<ColumnVo> l = query.list(ColumnVo.class);
		return l;
	}
	
	
	public List<ColumnVo> getColumnList(String parentColumnId) {
		Query query = QueryFactory.createQuery(
						"SELECT "
						+" COLUMN_ID as columnId, "
						+" COLUMN_PARENT as columnParent, "
						+" COLUMN_NAME as columnName, "
						+" COLUMN_INTRODUCE as columnIntroduce, "
						+" COLUMN_ENGLISH as columnEnglish, "
						+" COLUMN_SORT as columnSort, "
						+" COLUMN_URL as columnUrl, "
						+" COLUMN_TYPE as columnType, "
						+" COLUMN_STATE as columnState "
						+" FROM CMS_COLUMN "
						+" WHERE COLUMN_PARENT=? "
						+" ORDER BY COLUMN_SORT ASC ").setParameter(1, parentColumnId);
		List<ColumnVo> list = query.list(ColumnVo.class);
		return list;
	}
	public PageResult<ColumnVo> getPageList(int currentPage) {

		return QueryFactory.createQuery(
				"SELECT "
				+" COLUMN_ID as columnId, "
				+" COLUMN_PARENT as columnParent, "
				+" COLUMN_NAME as columnName, "
				+" COLUMN_INTRODUCE as columnIntroduce, "
				+" COLUMN_ENGLISH as columnEnglish, "
				+" COLUMN_SORT as columnSort, "
				+" COLUMN_URL as columnUrl, "
				+" COLUMN_TYPE as columnType, "
				+" COLUMN_STATE as columnState "
				+" FROM CMS_COLUMN "
				+" ORDER BY COLUMN_SORT ASC ").setCurrentPage(
						currentPage > 0 ? currentPage : 1).setPageRows(
						OrganizationConstants.PAGE_ROWS).pageList(
						ColumnVo.class);
	}
	
	
	public ColumnVo getColumn(String id) {
		Query query = QueryFactory.createQuery(
				"SELECT "
				+" COLUMN_ID as columnId, "
				+" COLUMN_PARENT as columnParent, "
				+" COLUMN_NAME as columnName, "
				+" COLUMN_INTRODUCE as columnIntroduce, "
				+" COLUMN_ENGLISH as columnEnglish, "
				+" COLUMN_SORT as columnSort, "
				+" COLUMN_URL as columnUrl, "
				+" COLUMN_TYPE as columnType, "
				+" COLUMN_STATE as columnState "
				+" FROM CMS_COLUMN "
				+" WHERE COLUMN_ID=? ").setParameter(1, id);
		return query.single(ColumnVo.class);
	}
	
	public ColumnVo getColumnVo(ColumnVo columnVo) {
		Query query = QueryFactory.createQuery(
						"SELECT "
						+" COLUMN_ID as columnId, "
						+" COLUMN_PARENT as columnParent, "
						+" COLUMN_NAME as columnName, "
						+" COLUMN_INTRODUCE as columnIntroduce, "
						+" COLUMN_ENGLISH as columnEnglish, "
						+" COLUMN_SORT as columnSort, "
						+" COLUMN_URL as columnUrl, "
						+" COLUMN_TYPE as columnType, "
						+" COLUMN_STATE as columnState "
						+" FROM CMS_COLUMN "
						+" WHERE COLUMN_NAME=? "
						+" AND COLUMN_PARENT=? "
						+" AND COLUMN_ENGLISH=? " )
							.setParameter(1, columnVo.getColumnName())
							.setParameter(2, columnVo.getColumnParent())
							.setParameter(3, columnVo.getColumnEnglish());
		
		return query.single(ColumnVo.class);
	}

	public boolean insertColumn(ColumnVo vo) {
		Query query = QueryFactory.createExecute("insert into CMS_COLUMN "
					+" values( "
					+" :columnId, "					//'主键',
					+" :columnParent, "	  			//'父栏目ID'
					+" :columnName, "	  			//'栏目名称'
					+" :columnIntroduce, "	  		//'栏目说明'
					+" :columnEnglish, "	  		//'英文标识'
					+" :columnSort, "	  			//'排序'
					+" :columnUrl, "	 			//'栏目网址'
					+" :columnType, "	 			//'栏目类型'
					+" :columnState) ",	 			//'状态'
						vo);	
		
		
		return query.execute() > 0;
	}
	
	public boolean modifyColumn(ColumnVo vo) {
		Query query = QueryFactory
				.createExecute(
						"update CMS_COLUMN "
				+" set column_parent=:columnParent, "
				+" column_name=:columnName, "
				+" column_introduce=:columnIntroduce, "
				+" column_english=:columnEnglish, "
				+" column_sort=:columnSort, "
				+" column_url=:columnUrl, "
				+" column_type=:columnType, "
				+" column_state=:columnState "
				+" where column_id=:columnId ",
						vo);
		return query.execute() > 0;
	}

	public boolean deleteColumn(String id) {
		Query query = QueryFactory.createExecute(
				"delete from miko_column where column_id=?").setParameter(1, id);
		return query.execute() > 0;
	}


	/**
	 * 保存机构排序
	 */
	public Boolean saveColumnSort(ColumnVo vo) {
		return QueryFactory.createExecute(
		"UPDATE CMS_COLUMN SET COLUMN_SORT="+vo.getColumnSort()+" WHERE COLUMN_ID='"+vo.getColumnId()+"'").execute() > 0;
	}	
}
